//
//  HSTopicDetailVM.m
//  HolyShit
//
//  Created by Robin on 2017/4/5.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSTopicDetailVM.h"

@interface HSTopicDetailVM ()

@property (nonatomic, copy) NSString *discussId;
@property (nonatomic, assign) NSUInteger page;

@end

@implementation HSTopicDetailVM

- (instancetype)initWithId:(NSString *)id
{
    if (self = [super init]) {
        _discussId = id;
        _page = 0;
    }
    return self;
}

- (RACCommand *)dataCommand{
    if(!_dataCommand){
        @weakify(self);
        NSString *url = [NSString stringWithFormat:@"%@%@/%@",AppHost, DiscussPath,self.discussId];
        _dataCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url  params:nil success:^(NSDictionary *dict) {
                if (![dict containsObjectForKey:@"included"]) {
                    return ;
                }
                @strongify(self);
                self.page = 0;
                HSForumListModel *data = [HSForumListModel modelWithDictionary:dict[@"data"]];
                self.posts = data.relationships.posts;
                NSArray *array = [NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"included"]];
                NSArray *topics = [self renewTopics:array];
                self.forums = topics.mutableCopy;
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _dataCommand;
}

- (RACCommand *)loadmoreCommand{
    if(!_loadmoreCommand){
        @weakify(self);
        NSString *url = [NSString stringWithFormat:@"%@%@/%@?page[offset]=%lu",AppHost, DiscussPath,self.discussId,(self.page + 1) * 20];
        _loadmoreCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url  params:nil success:^(NSDictionary *dict) {
                if (![dict containsObjectForKey:@"included"]) {
                    return ;
                }
                @strongify(self);
                NSArray *array = [NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"included"]];
                NSArray *topics = [self renewTopics:array];
                if (topics.count > 0) {
                    self.page += 1;
                    [self.forums addObjectsFromArray:topics];
                }
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _loadmoreCommand;
}

- (NSArray<HSForumListModel *> *)renewTopics:(NSArray<HSForumListModel *> *)topics
{
    NSMutableArray<HSForumListModel *> *__topics = @[].mutableCopy;
    NSMutableArray<HSForumListModel *> *__users = @[].mutableCopy;
    [topics enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.type isEqualToString:@"users"]) {
            [__users addObject:obj];
        }
        else if([obj.type isEqualToString:@"posts"]){
            [__topics addObject:obj];
        }
    }];
    [__topics enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [__users enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj2, NSUInteger idx2, BOOL * _Nonnull stop2) {
            if ([obj.relationships.user.data.id isEqualToString:obj2.id]) {
                obj.attributes.username = obj2.attributes.username;
                obj.attributes.avatarUrl = obj2.attributes.avatarUrl;
                *stop2 = YES;
            }
        }];
    }];
    return __topics;
}

@end
